package cn.kailangtu.myapplication.data;

import android.app.Activity;
import android.graphics.Color;
import android.os.SystemClock;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import cn.kailangtu.myapplication.R;
import cn.kailangtu.myapplication.consts.YuyinType;
import cn.kailangtu.myapplication.events.PlayYuyin;
import cn.kailangtu.myapplication.events.Shuaxing灯Event;
import cn.kailangtu.myapplication.events.刷新动能回收力度;
import cn.kailangtu.myapplication.events.刷新页面事件;
import cn.kailangtu.myapplication.specialview.ElectricView;
import cn.kailangtu.myapplication.tools.FomaterUtils;
import cn.kailangtu.myapplication.view.versionTwo.AutoMeterView;
import cn.kailangtu.serialport.CommonUtils;

public class yemian {

    List<yemian> lsMain;
    int 刷新灯间隔 = 500; //毫秒
    int 刷新数据间隔 = 200;


    List<Integer> lsPower = new ArrayList<>();

    private static yemian instance;
    public static  yemian getInstance()
    {
        if(instance == null)
        {
            instance = new yemian();
        }
        return instance;
    }

    private yemian()
    {


        lsPower.add(R.id.rbPower0);
        lsPower.add(R.id.rbPower1);
        lsPower.add(R.id.rbPower2);

        刷新灯();
        刷新页面();
    }


    public void refreshView(int pageIndex, Activity context) {


    }


//    lsMain = new ArrayList<>();
//    List<String> 电机状态 = new ArrayList<>();
//        电机状态.add("电机停止");
//        电机状态.add("允许启动");
//        电机状态.add("启动预警");
//        电机状态.add("电机运行");


    private boolean Address(int address ,int byteIndex,int bitIndex)
    {
        return CanData.getInstance().getDataBit(address,byteIndex,bitIndex);
    }

    public void  页面1 (int pageIndex, Activity context){
        Address.getInstance().getDdta();
        String danweisianshi="";
        if (Address.getInstance().前进挡位==true){
            danweisianshi="前进挡位";
        }else if(Address.getInstance().后退挡位==true){
            danweisianshi="后退挡位";
        } else {
            danweisianshi="空挡";
        }

        TextView tvDanweiR = context.findViewById(R.id.dengweixianshi);
        if(tvDanweiR.getTag() == null)
        {
            tvDanweiR.setText(danweisianshi);
        }
        else
        {
            TextView tvN = context.findViewById(R.id.dengweixianshiN);
            TextView tvD = context.findViewById(R.id.dengweixianshiD);
            if(danweisianshi.equals("空挡"))
            {
                tvD.setTextColor(Color.GRAY);
                tvN.setTextColor(Color.GREEN);
                tvD.setTextSize(42);
                tvN.setTextSize(56);
                tvDanweiR.setTextSize(42);
                tvDanweiR.setTextColor(Color.GRAY);
            }
            else   if(danweisianshi.equals("前进挡位"))
            {
                tvD.setTextColor(Color.GREEN);
                tvN.setTextColor(Color.GRAY);
                tvD.setTextSize(56);
                tvN.setTextSize(42);
                tvDanweiR.setTextSize(42);
                tvDanweiR.setTextColor(Color.GRAY);
            }
            else   if(danweisianshi.equals("后退挡位"))
            {
                tvD.setTextColor(Color.GRAY);
                tvN.setTextColor(Color.GRAY);
                tvD.setTextSize(42);
                tvN.setTextSize(42);
                tvDanweiR.setTextSize(56);
                tvDanweiR.setTextColor(Color.GREEN);
            }
        }


       // Tool.getInstance().输入框(danweisianshi,R.id.dengweixianshi,context);
        boolean m1;
        if(Address.getInstance().右转向灯==true||Address.getInstance().双闪灯==true){
            m1=true;
        }else {
            m1=false;
        }
       // Tool.getInstance().标签动画(m1,闪烁,R.id.youzhuanxiangdeng,context);
//        if(Address.getInstance().右转向灯==true&&闪烁==1&&Address.getInstance().左转向灯==false){
//            EventBus.getDefault().post(new PlayYuyin(YuyinType.TURN_kai));
//        }else { EventBus.getDefault().post(new PlayYuyin(YuyinType.TURN_guan));}
//
//        if(Address.getInstance().左转向灯==true&&闪烁==1&&Address.getInstance().左转向灯==false){
//            EventBus.getDefault().post(new PlayYuyin(YuyinType.TURN_kai));
//        }else { EventBus.getDefault().post(new PlayYuyin(YuyinType.TURN_guan));}
//        if(闪烁==1&&Address.getInstance().左转向灯==true){
//            EventBus.getDefault().post(new PlayYuyin(YuyinType.TURN_kai));
//        }else { EventBus.getDefault().post(new PlayYuyin(YuyinType.TURN_guan));}
        boolean m2;
        if(Address.getInstance().左转向灯==true||Address.getInstance().双闪灯==true){
            m2=true;
        }else {
            m2=false;
        }
       // Tool.getInstance().标签动画(m2,闪烁,R.id.zuozhuanxiangdeng,context);
       // Tool.getInstance().标签动画(Address.getInstance().双闪灯,闪烁,R.id.shuangshan,context);
        Tool.getInstance().标签动画(Address.getInstance().远光灯,R.id.yuanguandeng,context);
        Tool.getInstance().标签动画(Address.getInstance().近光灯,R.id.jingguandeng,context);
        Tool.getInstance().标签动画(Address.getInstance().后雾灯,R.id.wudeng,context);
        Tool.getInstance().标签动画(Address.getInstance().示廓灯,R.id.sikuodeng,context);
        Tool.getInstance().标签动画(!Address.getInstance().门关,R.id.men,context);
        Tool.getInstance().标签动画(!Address.getInstance().安全带系,R.id.anquandai,context);
        boolean shousa;
        if(Address.getInstance().手刹==false){
            shousa=true;
        }else { shousa=false;}
        Tool.getInstance().标签动画(shousa,R.id.shousadeng,context);
        Tool.getInstance().标签动画(!Address.getInstance().脚刹, R.id.jiaosa,context);
        Tool.getInstance().标签动画(!Address.getInstance().座位开关,R.id.anquanzuoyi,context);
        Tool.getInstance().标签动画(Address.getInstance().龟速,R.id.guisu,context);
        Tool.getInstance().标签动画(Address.getInstance().真空泵报警,R.id.CAN381H2S0,context);
        Tool.getInstance().标签动画(Address.getInstance().刹车油液位低,R.id.CAN381H2S1,context);

        String anquanjiejin="";
        String anquanjiejin12="";
        if (Address.getInstance().安全接近==true){
            anquanjiejin="安全接近";
            if(Address.getInstance().安全接近向前==true){
                anquanjiejin12="向前对接";
            }else if(Address.getInstance().安全接近向后==true){
                anquanjiejin12="向后对接";
            }else {
                anquanjiejin12="";}
        }else {
            anquanjiejin="";
            anquanjiejin12="";
        }

        TextView tvJiejinStatus = context.findViewById(R.id.anquanjiejin);
        TextView tvJiejinValue = context.findViewById(R.id.anquanjiejin12);
        if(tvJiejinValue.getTag() != null && tvJiejinValue.getTag().toString().equals("U0"))
        {
            if(anquanjiejin.length()>0)
            {
                tvJiejinStatus.setText("是");
                tvJiejinStatus.setTextColor(Color.GREEN);

            }
            else
            {
                tvJiejinStatus.setText("否");
                tvJiejinStatus.setTextColor(Color.WHITE);
            }
            if(anquanjiejin12.length()>0) {
                tvJiejinValue.setText(anquanjiejin12);
            }
            else
            {
                tvJiejinValue.setText("没有状态");
            }
        }
        else
        {
            tvJiejinStatus.setText(anquanjiejin);
            tvJiejinValue.setText(anquanjiejin12);
        }


        TextView tvYiyayouyali = context.findViewById(R.id.dianjiwendu);
        if(tvYiyayouyali.getTag() == null)
        {
            tvYiyayouyali.setText("液压油压力"+ CommonUtils.floatToString(Address.getInstance().液压油压力,2)+"MPa");
        }
        else
        {
            tvYiyayouyali.setText(CommonUtils.floatToString(Address.getInstance().电池仓温度,1)+"°C");
        }


        TextView tvZongdianya = context.findViewById(R.id.dianchidianya);
        if(tvZongdianya.getTag() == null)
        {
            tvZongdianya.setText("总电压"+ Address.getInstance().电池组总电压+"V");
        }
        else
        {
            tvZongdianya.setText(CommonUtils.floatToString(Address.getInstance().电池组总电压,1)+"V");
        }

        TextView tvZongdianliu = context.findViewById(R.id.dianchidianliu);
        if(tvZongdianliu.getTag() == null)
        {
            tvZongdianliu.setText("总电流"+ CommonUtils.floatToString( Address.getInstance().电池组总电流,1)+"A");
        }
        else
        {
            tvZongdianliu.setText(CommonUtils.floatToString(Address.getInstance().电池组总电流,1)+"A");
            ElectricView.getInstance().setElectricValue( new Float(Address.getInstance().电池组总电流).intValue());
        }

        //Tool.getInstance().输入框(anquanjiejin,R.id.anquanjiejin,context);
        //Tool.getInstance().输入框(anquanjiejin12,R.id.anquanjiejin12,context);
        //Tool.getInstance().输入框("液压油压力"+ CommonUtils.floatToString(Address.getInstance().液压油压力,2)+"MPa",R.id.dianjiwendu,context);
        //Tool.getInstance().输入框("总电压"+ Address.getInstance().电池组总电压+"V",R.id.dianchidianya,context);
        //Tool.getInstance().输入框("总电流"+ Address.getInstance().电池组总电流+"A",R.id.dianchidianliu,context);
        Tool.getInstance().输入框(""+ Address.getInstance().剩余容量+"%",R.id.dianchidianliang,context);
        View autoMeterView =  context.findViewById(R.id.dashView2);
        if(autoMeterView instanceof cn.kailangtu.myapplication.view.versionTwo.AutoMeterView)
        {
            ((cn.kailangtu.myapplication.view.versionTwo.AutoMeterView)autoMeterView).setDegreeOffset(Address.getInstance().剩余容量);
        }
        else if(autoMeterView instanceof cn.kailangtu.myapplication.view.AutoMeterView)
        {
            ((cn.kailangtu.myapplication.view.AutoMeterView)autoMeterView).setDegreeOffset(Address.getInstance().剩余容量);
        }
        if(Address.getInstance().剩余容量<31&&Address.getInstance().剩余容量>21){
            if(canPlayPower(YuyinType.POWER_30)) {
               // EventBus.getDefault().post(new PlayYuyin(YuyinType.POWER_30));
            }
        }else if(Address.getInstance().剩余容量<21&&Address.getInstance().剩余容量>11){
            if(canPlayPower(YuyinType.POWER_20)) {
               // EventBus.getDefault().post(new PlayYuyin(YuyinType.POWER_20));
            }
        }else if(Address.getInstance().剩余容量<11){
            if(canPlayPower(YuyinType.POWER_10)) {
               // EventBus.getDefault().post(new PlayYuyin(YuyinType.POWER_10));
            }
        }

        TextView tvDianchiMax = context.findViewById(R.id.dianchiMAX);
        TextView tvDianchiMin = context.findViewById(R.id.dianchiMIN);
        if(tvDianchiMax.getTag() == null)
        {
            tvDianchiMax.setText("最高温度"+ Address.getInstance().最高电芯温度+"°C");
            tvDianchiMin.setText("最低温度"+ Address.getInstance().最低电芯温度+"°C");
        }
        else{
            tvDianchiMax.setText(CommonUtils.floatToString(Address.getInstance().最高电芯温度,1)+"°C");
            tvDianchiMin.setText(CommonUtils.floatToString(Address.getInstance().最低电芯温度,1)+"°C");
        }


        TextView tvLiCheng = context.findViewById(R.id.xinshilicheng);
        TextView tvXiaoshiji = context.findViewById(R.id.xiashiji);
        if(tvLiCheng.getTag() == null)
        {
            tvLiCheng.setText("行驶里程"+ CommonUtils.floatToString(Address.getInstance().行驶里程[0],2) +"km");
        }
        else
        {
            tvLiCheng.setText(CommonUtils.floatToString(Address.getInstance().行驶里程[0],0) +"km");
        }

        if(tvXiaoshiji.getTag() == null)
        {
            tvXiaoshiji.setText("小时计"+ CommonUtils.floatToString(Address.getInstance().小时计[0],0) +"h");
        }
        else{
            tvXiaoshiji.setText(CommonUtils.floatToString(Address.getInstance().小时计[0],0) +"h");
        }

        int newSu = new Float(Address.getInstance().车速).intValue();
        Tool.getInstance().输入框(""+ newSu,R.id.buttonsudu,context);
        //Tool.getInstance().仪表盘( Address.getInstance().车速,R.id.dashView,context);
        //Tool.getInstance().仪表盘( Address.getInstance().车速,R.id.dashView,context);
       // Tool.getInstance().输入框("行驶里程"+ CommonUtils.floatToString(Address.getInstance().行驶里程[0],2) +"km",R.id.xinshilicheng,context);
        //Tool.getInstance().输入框("小时计"+ CommonUtils.floatToString(Address.getInstance().小时计[0],0) +"h",R.id.xiashiji,context);
       // Tool.getInstance().输入框("最高温度"+ Address.getInstance().最高电芯温度+"%",R.id.TextView_time,context);

    }



    public void  页面2 (int pageIndex, Activity context){
        Address.getInstance().getDdta();

        Tool.getInstance().输入框("控制器故障码:    "+ Address.getInstance().行走控制器错误代码+"",R.id.kogzhiqiguzhanma,context);
        Tool.getInstance().输入框("最高单体电压:    "+ Address.getInstance().最高单体电压+"mV",R.id.zuigaodantidianya,context);
        Tool.getInstance().输入框("最高单体位置:    "+ Address.getInstance().最高单体位置_VIEW,R.id.zuigaodantiweizhi,context);
        Tool.getInstance().输入框("最低单体电压:    "+ Address.getInstance().最低单体电压+"mV",R.id.zuididantidianya,context);
        Tool.getInstance().输入框("最低单体位置:    "+ Address.getInstance().最低单体位置_VIEW,R.id.zuididantiweizhi,context);
        Tool.getInstance().输入框("最高电芯温度:    "+ CommonUtils.floatToString(Address.getInstance().最高电芯温度,1)+"℃",R.id.zuigaodianxinwendu,context);
        Tool.getInstance().输入框("最高温度位置:    "+ Address.getInstance().最高温度位置_VIEW,R.id.zuigaodianxinweizhi,context);
        Tool.getInstance().输入框("最低电芯温度:    "+ CommonUtils.floatToString(Address.getInstance().最低电芯温度,1)+"℃",R.id.zuididianxinwendu,context);
        Tool.getInstance().输入框("最低温度位置:    "+ Address.getInstance().最低温度位置_VIEW,R.id.zididainxiweiduweizhi,context);
        Tool.getInstance().输入框("平均电芯温度:    "+ Address.getInstance().最低单体位置+"℃",R.id.pingjunweidu,context);
        Tool.getInstance().输入框("单体过压:    "+ Address.getInstance().单体过压,R.id.dantiguoya,context);
        Tool.getInstance().输入框("单体欠压:    "+ Address.getInstance().单体欠压,R.id.dantiqianya,context);
        Tool.getInstance().输入框("单体欠压:    "+ Address.getInstance().总电压过压,R.id.zongdianyaguoya,context);
        Tool.getInstance().输入框("总电压欠压:   "+ Address.getInstance().总电压欠压,R.id.zongdianyaqianya,context);
        Tool.getInstance().输入框("单体压差大:   "+ Address.getInstance().单体压差大,R.id.dantiyachada,context);
        Tool.getInstance().输入框("放电过流:    "+ Address.getInstance().放电过流,R.id.fandianguoliu,context);
        Tool.getInstance().输入框("充电过流:    "+ Address.getInstance().充电过流,R.id.chongdianguoliu,context);
        Tool.getInstance().输入框("温度过高:    "+ Address.getInstance().温度过高,R.id.wenduguogao,context);
        Tool.getInstance().输入框("温度过低:    "+ Address.getInstance().温度过低,R.id.wenduguodi,context);
        Tool.getInstance().输入框("温差过大:    "+ Address.getInstance().温差过大,R.id.wenchaguoda,context);
        Tool.getInstance().输入框("SOC过低:    "+ Address.getInstance().SOC过低,R.id.SOCguodi,context);
        Tool.getInstance().输入框("绝缘过低:    "+ Address.getInstance().绝缘过低,R.id.jueyuanguodi,context);
        Tool.getInstance().输入框("高压互锁故障: "+ Address.getInstance().高压互锁故障,R.id.gaoyahusuoguzhi,context);
        Tool.getInstance().输入框("外部通讯故障: "+ Address.getInstance().外部通讯故障,R.id.waibutongxuguzhang,context);
        Tool.getInstance().输入框("内部通讯故障: "+ Address.getInstance().内部通讯故障,R.id.leibutongxuguzhng,context);


        EventBus.getDefault().post(new 刷新动能回收力度(Address.getInstance().动能回收力度));


    }



    long lastPlayPowerTime = 0;
    int lastPowerType = -1;
    private boolean canPlayPower(int powerType)
    {
        if(System.currentTimeMillis() - lastPlayPowerTime > 30000 || (lastPowerType != powerType))
        {
            lastPlayPowerTime = System.currentTimeMillis();
            lastPowerType = powerType;
            return true;
        }
        return false;
    }

//    private void ddd()
//    {
//        RelativeLayout rl = new RelativeLayout();
//        int count = rl.getChildCount();
//        for(int i = 0;i<count;i++)
//        {
//            View v = rl.getChildAt(i);
//            String name =  v.getTag().toString();
//            if(name.startsWith("CAN"))
//            {
//                // CAN281H0S0
//                int add = 281;
//                int byteIndex = 0;
//                int bitIndex = 0;
//                int sourceId = v.getId();
//            }
//        }
//        rl.getChildAt(0)
//    }


      int 闪烁;
    private void 刷新灯()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true)
                {
                    if (闪烁==1){
                        闪烁=0;
                    }else {
                        闪烁=1;
                    }
                   // EventBus.getDefault().post(new Shuaxing灯Event(闪烁));
                    SystemClock.sleep(刷新灯间隔);
                }
            }
        }).start();
    }

    private void 刷新页面()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true)
                {
                    EventBus.getDefault().post(new 刷新页面事件());
                    SystemClock.sleep(刷新数据间隔);
                }
            }
        }).start();
    }

}
